package nc.gk.gkxm.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import io.jsonwebtoken.Claims;
import nc.gk.gkxm.mapper.TopicMapper;
import nc.gk.gkxm.mapper.UserMapper;
import nc.gk.gkxm.model.dto.TopicDto;
import nc.gk.gkxm.model.entity.Option;
import nc.gk.gkxm.model.entity.Topic;
import nc.gk.gkxm.model.entity.User;
import nc.gk.gkxm.model.other.JwtProperties;
import nc.gk.gkxm.model.vo.OptionVO;
import nc.gk.gkxm.model.vo.PersonlibVo;
import nc.gk.gkxm.model.vo.TopicVO;
import nc.gk.gkxm.service.OptionService;
import nc.gk.gkxm.service.SortService;
import nc.gk.gkxm.service.TopicService;
import nc.gk.gkxm.utils.jwt.JwtUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

@Service
public class TopicServiceImpl extends ServiceImpl<TopicMapper, Topic> implements TopicService {
    @Autowired
    private SortService sortService;
    @Autowired
    private OptionService optionService;

    @Resource
    private TopicMapper topicMapper;
    @Resource
    private JwtProperties jwtProperties;
    @Resource
    private UserMapper userMapper;
    @Override
    public List<TopicVO> getAllTopic() throws Exception {
       List<Topic> topics = baseMapper.selectList(null);
       if(topics == null && topics.size() < 1){
           throw new Exception("没有任何题目信息，请联系管理员进行添加");
       }
       List<TopicVO> vos = new ArrayList<>();
       for(Topic topic : topics){
           TopicVO vo = new TopicVO();
           vo.setTopic(topic.getTopic());
           vo.setNumber(topic.getNumber());
           vo.setTopicid(topic.getTopicid());
           List<OptionVO> options = optionService.getOptionByTopicId(topic.getTopicid());
           if(options != null && options.size() > 3) {
               vo.setOptions(options);
               vos.add(vo);
           }
       }
        return vos;
    }

    @Transactional
    @Override
    public void insertTopic(TopicDto topicDto) {
        if(topicDto != null){
//            Topic topic = new Topic();
//            topic.setTopic(aggvo.getTopic());
//            topic.setAnswer(aggvo.getAnswer());
//            topic.setTanalyze(aggvo.getTanalyze());
//            topic.setSort(aggvo.getSort());
//            topic.setTopic(aggvo.getTopic());

            //           baseMapper.insert(topic);
            topicMapper.insertTopic(topicDto);
            for (Option option : topicDto.getOptions()) {
                option.setTopicid(topicDto.getTopicid());
                topicMapper.insertOption(option);

            }


        }
    }

    @Override
    public List<TopicVO> getAllByTopicId(Integer id) {

        return null;
    }

    @Override
    public List<TopicVO> getAllBySortId(Integer[]  sortIds) {
//        是否为空
        if(sortIds == null || sortIds.length < 1){
            throw new RuntimeException("没有相应的类型");
        }


        return topicMapper.getAllBySortId(sortIds);
    }

    @Override
    public List<TopicVO> getAllByContent(String content) {
        //TODO 分页
        return null;
    }

    @Override
    public Map<String, String> getAnalyze(Integer topicId) {
        if(topicId == null) {
            throw new RuntimeException("没有相应的题目");
        }
        Map<String,String> map=topicMapper.getAnalyze(topicId);
        if(map ==null){
            throw new RuntimeException("没有相应的题目解析");
        }
        if(!map.containsKey("tanalyze")){
            map.put("tanalyze","暂无解析");
        }
        return map;
    }

    @Override
    public Integer getAnswer(TopicDto topicDto) {
        if(topicDto.getTopicid()==null||topicDto.getTopic()==""){
            throw new RuntimeException("获取答案异常，请联系管理员");
        }
//        效验答案
        Map<String, String> map = this.getAnalyze(topicDto.getTopicid());
        if(topicDto.getTopic().equals(map.get("answer"))) {
           return 1;
        }
        return 2;
    }

    @Transactional
    @Override
    public void updateTopic(TopicDto topicDto) {
        topicMapper.updateTopic(topicDto);
        this.deletebOptionBytopicId(topicDto.getTopicid());

        for (Option option : topicDto.getOptions()) {
             option.setTopicid(topicDto.getTopicid());
            topicMapper.insertOption(option);
        }

    }

    @Override
    public void deletebOptionBytopicId(Integer topicId) {
        topicMapper.deletebOptionBytopicId(topicId);
    }

    @Transactional
    @Override
    public void deleteByTopicId(Integer topicId) {
        topicMapper.deleteByTopicId(topicId);
    }

    @Override
    public PersonlibVo getOneRandomTopic(String token) {
        Claims claims = JwtUtil.parseJWT(jwtProperties.getUserSecretKey(), token);
        Long userId = Long.valueOf(claims.get("userId").toString());
//        获取用户信息
        User user=userMapper.getUserByCode(String.valueOf(userId));
//      获取随机一条数据
        PersonlibVo data=topicMapper.getOneRandomTopic(user.getCode());

        return data;
    }


}
